package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseSoundLightAlarm;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseSoundLightAlarmService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/baseSoundLightAlarm")
@Api(value = "BaseSoundLightAlarmController", tags = "声光报警器管理")
public class BaseSoundLightAlarmController {
    private final BaseSoundLightAlarmService baseSoundLightAlarmService;
    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     *
     * @param page                分页对象
     * @param baseSoundLightAlarm 声光报警器
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    public R getBaseSoundLightAlarmPage(Page page, BaseSoundLightAlarm baseSoundLightAlarm) {
        return R.ok(baseSoundLightAlarmService.page(page, Wrappers.query(baseSoundLightAlarm)));
    }

    /**
     * 模糊分页查询
     *
     * @param baseVo
     * @return
     */
    @ApiOperation(value = "模拟分页查询", notes = "模拟分页查询")
    @PostMapping("/vaguePage")
    public R vaguePage(@RequestBody BaseVo baseVo) {
        return R.ok(baseSoundLightAlarmService.vaguePage(baseVo));
    }

    /**
     * 通过id查询声光报警器
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(baseSoundLightAlarmService.getById(id));
    }

    /**
     * 新增语音合成播放表
     *
     * @param baseSoundLightAlarm 声光报警器
     * @return R
     */
    @ApiOperation(value = "新增声光报警器", notes = "新增声光报警器")
    @PostMapping
    public R save(@RequestBody BaseSoundLightAlarm baseSoundLightAlarm) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSoundLightAlarm.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseSoundLightAlarm.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseSoundLightAlarm.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseSoundLightAlarm.setManufacturer(manufacturer.getManufacturerFullName());
        baseSoundLightAlarm.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseSoundLightAlarmService.save(baseSoundLightAlarm));
    }

    /**
     * 修改语音合成播放表
     *
     * @param baseSoundLightAlarm 语音合成播放表
     * @return R
     */
    @ApiOperation(value = "修改声光报警器", notes = "修改声光报警器")
    @PutMapping
    public R updateById(@RequestBody BaseSoundLightAlarm baseSoundLightAlarm) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseSoundLightAlarm.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseSoundLightAlarm.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseSoundLightAlarm.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseSoundLightAlarm.setManufacturer(manufacturer.getManufacturerFullName());
        baseSoundLightAlarm.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseSoundLightAlarmService.updateById(baseSoundLightAlarm));
    }

    /**
     * 通过id删除语音合成播放表
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除语音合成播放表声光报警器", notes = "通过id删除声光报警器")
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id) {
        return R.ok(baseSoundLightAlarmService.removeById(id));
    }


    /**
     * 导出excel 表格
     *
     * @param baseSoundLightAlarm 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseSoundLightAlarm> export(BaseSoundLightAlarm baseSoundLightAlarm) {
        return baseSoundLightAlarmService.list(Wrappers.query(baseSoundLightAlarm));
    }

    /**
     * 修改状态
     *
     * @param baseSoundLightAlarm
     * @return R
     */
    @ApiOperation(value = "修改语音合成播放状态信息", notes = "修改语音合成播放状态信息")
    @PutMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseSoundLightAlarm baseSoundLightAlarm) {
        return R.ok(baseSoundLightAlarmService.updateStateById(baseSoundLightAlarm));
    }
}
